﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using AO.Core;

namespace ZoneEngine.Functions
{
    class Function_teleportproxy : FunctionPrototype
    {
        public new int FunctionNumber = 53082;
        
        public new string FunctionName = "teleportproxy";
        
        public override int ReturnNumber()
        { 
            return FunctionNumber; 
        }
        
        public override bool Execute(Dynel Self, Dynel Caller, object Target, object[] Arguments)
        {
            lock (Self)
            {
                lock (Caller)
                {
                    lock (Target)
                    {
                        return FunctionExecute(Self, Caller, Target, Arguments);
                    }
                }
            }
        }
        
        public override string ReturnName()
        {
            return FunctionName;
        }



        public bool FunctionExecute(Dynel Self, Dynel Caller, object Target, object[] Arguments)
        {
            Identity pfinstance = new Identity();
            Identity id2 = new Identity();
            Identity id3 = new Identity();
            Client cli = ((Character)Self).client;
            if (Target is Statels.Statel)
            {
                pfinstance.Type = Int32.Parse((string)Arguments[0]);
                pfinstance.Instance = Int32.Parse((string)Arguments[1]);
                id2.Type = Int32.Parse((string)Arguments[2]);
                id2.Instance = Int32.Parse((string)Arguments[3]);
                id3.Type = Int32.Parse((string)Arguments[4]);
                id3.Instance = Int32.Parse((string)Arguments[5]);
            }
            else
            {
                // Shouldnt happen ever, as far as i know only Statels do ProxyTeleports (perhaps GM's can too tho)
                pfinstance.Type = (Int32)Arguments[0];
                pfinstance.Instance= (Int32)Arguments[1];
                id2.Type = (Int32)Arguments[2];
                id2.Instance = (Int32)Arguments[3];
                id3.Type = (Int32)Arguments[4];
                id3.Instance = (Int32)Arguments[5];
            }
            SqlWrapper ms = new SqlWrapper();
            DataTable dt = ms.ReadDT("SELECT * from proxydestinations WHERE playfield=" + pfinstance.Instance);
            if (dt.Rows.Count == 0)
            {
#if DEBUG
                                cli.SendChatText("No Destination found for playfield " + pfinstance.Instance);
                                foreach (string arg in Arguments)
                                {
                                    cli.SendChatText("Argument: " + arg);
                                }
#endif
            }
            else
            {
                AOCoord a = new AOCoord();
                a.x = (Single)dt.Rows[0][1];
                a.y = (Single)dt.Rows[0][2];
                a.z = (Single)dt.Rows[0][3];
                Quaternion q = new Quaternion(0, 0, 0, 0);
                q.x = (Single)dt.Rows[0][4];
                q.y = (Single)dt.Rows[0][5];
                q.z = (Single)dt.Rows[0][6];
                q.w = (Single)dt.Rows[0][7];
                int instance = (Int32)((Statels.Statel)Target).Instance;
                // TODO: 1=GS need to change that later to a crowdbalancing system
                cli.TeleportProxy(a, q, pfinstance.Instance, pfinstance, 1, instance, id2, id3); 
            }
            return true;
        }
    }
}
